package com.zhuiyun.pm.client.home.service.impl;

import com.zhuiyun.pm.client.home.mapper.AnnouncementMapper;
import com.zhuiyun.pm.client.home.pojo.ClientAnnouncement;
import com.zhuiyun.pm.client.home.pojo.dto.ClientAnnouncementDTO;
import com.zhuiyun.pm.client.home.service.AnnouncementService;
import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;
import com.zhuiyun.pm.result.CommonResult;
import com.zhuiyun.pm.utils.RedisKeyExpirationListener;
import com.zhuiyun.pm.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description 用户端——公告
 * @ClassName AnnouncementServiceImpl
 * @Author HH
 * @Date 2023/3/13 13:38
 **/
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    AnnouncementMapper announcementMapper;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    RedisKeyExpirationListener redisKeyExpirationListener;

    String key = "announcementPageViews:announcementPageViews";
    String key2 = "announcementPageViews_2:announcementPageViews";
    /**
    * @Description 查询公告
    * @Author HH
    * @Date 2023/3/16 18:37
    * @Return com.zhuiyun.pm.result.CommonResult
    * @param address
* @param pageRequest
    */
    
    @Override
    public CommonResult selectAnnouncementList(String address, PageRequest pageRequest) {
        MyPageHelper.startPage(pageRequest);
        List<ClientAnnouncementDTO> list = announcementMapper.selectAnnouncementList(address);
        for (int i = 0; i < list.size(); i++) {
            if(redisUtil.hasKey(key2+list.get(i).getId())){
                list.get(i).setPageViews((Integer) redisUtil.get(key2+list.get(i).getId()));
            }else {
                redisUtil.set(key + list.get(i).getId(), list.get(i).getPageViews(), 15);
                redisUtil.set(key2 + list.get(i).getId(), list.get(i).getPageViews());
                list.get(i).setPageViews((Integer) redisUtil.get(key2 + list.get(i).getId()));
            }
        }
        PageResult pageResult = MyPageHelper.pageInfo(list);
        return CommonResult.ok(pageResult);
    }

    /**
     * @Description 公告浏览量自增
     * @Author HH
     * @Date 2023/3/15 21:24
     * @Return com.zhuiyun.pm.result.CommonResult
     * @param clientAnnouncement
     */
    @Override
    public CommonResult announcementPageViewsIncr(ClientAnnouncement clientAnnouncement) {
        String kk = key+clientAnnouncement.getId();
        String kk2 = key2+clientAnnouncement.getId();
        if(redisUtil.hasKey(kk2)){
            redisUtil.incr(kk2,1);
        }else {
            redisUtil.set(kk, clientAnnouncement.getPageViews(),15);
            redisUtil.set(kk2, clientAnnouncement.getPageViews());
            redisUtil.incr(kk2,1);
        }
        return CommonResult.ok();
    }

    /**
     * @Description 根据token查询用户信息
     * @Author HH
     * @Date 2023/3/19 19:16
     * @Return com.zhuiyun.pm.result.CommonResult
     * @param token
     */
    @Override
    public CommonResult getUserInfoByToken(String token) {
        Object o = redisUtil.get(token);
        if(o != null){
            return CommonResult.ok(o);
        }else {
            return CommonResult.error("查询失败");
        }
    }

}
